package com.yt.product.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.common.utils.StrUtils;
import com.yt.product.models.bo.ProductCategoryBO;
import com.yt.product.models.bo.ProductCategoryDeleteRequest;
import com.yt.product.models.bo.ProductCategorySaveBO;
import com.yt.product.models.bo.ProductCategoryStatusUpdateRequest;
import com.yt.product.models.dto.ProductCategoryDTO;
import com.yt.product.models.service.IProductCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * 产品类别信息控制器
 *
 * @author lianyadong
 * @Date 2025-09-03 14:16:35
 */

@RestController
@RequestMapping("/cms/productCategory")
@Slf4j
@Api(value = "ProductCategoryController",tags = "产品类别信息接口")
public class ProductCategoryController {

    @DubboReference
    private IProductCategoryService productCategoryService;

    @ApiOperation(value = "添加产品类别信息",notes = "添加产品类别信息")
    @PostMapping(value = "/insert")
    public Result insert(ProductCategorySaveBO saveBO) {
        boolean flag = productCategoryService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存产品类别信息",notes = "保存产品类别信息")
    public Result save(ProductCategorySaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改产品类别信息",notes = "修改产品类别信息")
    @PostMapping("/update")
    public Result update(ProductCategorySaveBO saveBO){
        ProductCategoryDTO dto = new ProductCategoryDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = productCategoryService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    // 修改状态接口重构
    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态", notes = "修改状态")
    public Result updateStatus(@Valid @RequestBody ProductCategoryStatusUpdateRequest request) {
        ProductCategoryDTO dto = new ProductCategoryDTO();
        dto.setId(request.getId());
        dto.setStatus(request.getStatus());
        boolean flag = productCategoryService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    // 删除接口重构
    @PostMapping("/delete")
    @ApiOperation(value = "删除产品类别信息", notes = "删除产品类别信息")
    public Result delete(@Valid @RequestBody ProductCategoryDeleteRequest request) {
        boolean flag = productCategoryService.deleteById(request.getId());
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }


    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除产品类别信息",notes = "批量删除产品类别信息")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        productCategoryService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ProductCategoryDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ProductCategoryDTO dto = productCategoryService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "产品类别信息列表",notes = "产品类别信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<ProductCategoryDTO>> queryPageList(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            ProductCategoryBO bo) {  // BO参数放在最后

        Wrapper<ProductCategoryDTO> wrapper = new EntityWrapper<>();
        if (bo.getStatus() != null){
            wrapper.eq("status", bo.getStatus());
        }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());
        }

        // 使用请求参数中的分页信息创建Page对象
        Page<ProductCategoryDTO> page = new Page<>(pageNo, pageSize);
        page = productCategoryService.selectPage(page, wrapper);

        return new Result<>(page);
    }

}
